home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 794 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  71 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: HELP! File Pointers
  5. Message-ID: <1996Jan9.083549.27165@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <4csr4c$fem@newsbf02.news.aol.com>
  11. Date: Tue, 9 Jan 1996 08:35:49 GMT
  12.  
  13. In article <4csr4c$fem@newsbf02.news.aol.com>, roberino@aol.com (Roberino) writes:
  14. >I am currently trying to keep one file open while opening other
  15. >files one at a time using a separate file pointer.  However, as
  16. >soon as I read a line from the second file, the first file pointer
  17. >somehow gets destroyed and set to some position in the newly
  18. >opened file.  Has anyone else encountered this?  And if so,
  19. >is there a solution? (i.e. A way to protect the first file pointer
  20. >from being overwritten.)
  21. >
  22. >Here are the steps I am performing:
  23. >
  24. >void main()
  25.  
  26. Ah! You will see flames for this. Change it to "int main()" and
  27. and do a return at the end of the function.
  28.  
  29. >{
  30. >    FILE *File1;
  31. >    FILE *File2;
  32. >
  33. >    File1 =   fopen("FILENAME", "r+");
  34. >    
  35. >    /* loop through lines in File1 using fgets() */
  36. >
  37. >    if (Condition) /* just indicating some condition was met */
  38. >    {
  39. >        File2 = fopen("FILENAME2", "r+");
  40. >      
  41. >        fgets(Line, File2); <----- As soon as this occurs, File1 gets
  42. >                                          wiped out.  Why?
  43. >    }
  44. >}
  45.  
  46. Well, the most obvious possibility is that you are using the wrong
  47. parameters for fgets. It takes three parameters and is declared as
  48. follows (at least on my Sun system):
  49.  
  50.     char *fgets (char *s, int n, FILE *stream)
  51.  
  52. So, you should:
  53.  
  54.     - make sure that "Line" is large enough for the lines you
  55.       expect to read.
  56.  
  57.     - call "fgets" with the right parameters.
  58.  
  59. I'd also suggect finding a better compiler - one that properly warns
  60. you about this error in the call.
  61.  
  62.                                 Z
  63.  
  64.  
  65.  
  66. -- 
  67. -------------------------------------------------------------------------
  68. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  69. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  70. -------------------------------------------------------------------------
  71.